'''
Created on May 22, 2012

@author: TANDA
'''
import re
import string
import urllib2

def q1():
    elements = [1, 4, 5, 6, 2, 6, 2]
    l = list(set(sorted(elements)))
    
    #l = reduce(lambda x, y:x if y in x else x + [y], sorted(elements), [])
    print("{!r}".format(l))
    
def q2():
    a = [1, 1, 1, 1, 2, 3, 4]
    b = [1, 4, 4, 4, 5, 6, 7]
    l = sorted(list(set(a) & set(b)))
    #l = reduce(lambda x, y:x if y in x else x + [y], filter(lambda x: x in b, a) , [])
    print("{!r}".format(l))

def q3():
    a = [1, 1, 1, 1, 2, 3, 4]
    b = [1, 4, 4, 4, 5, 6, 7]
    sorted(list(set(a) ^ set(b)))
    l = reduce(lambda x, y:x if y in x else x + [y], filter(lambda x: x not in b, a) + filter(lambda x: x not in a, b), [])
    
    print l

def q4():
    a = [1, 1, 1, 1, 2, 3, 4]
    b = [1, 4, 4, 4, 5, 6, 7]
    l = sum([i * j for (i, j) in zip(a, b)])
    print l
        
def q5():
    text = 'a12\n\n\n\t12b'
    s = "".join(re.findall('[a-zA-Z]*', text))
    print s

def q6():
    temp = re.findall('([0-9]*\.?[0-9])(?=&#176;C)', urllib2.urlopen('http://www.02ws.com/station.php').read())[0]
    print temp
    
def q7():
    url = 'http://search.walla.co.il/?q={term}'
    search = 'hello!!!'
    
    #re.findall(r'(?<=href=")[\'"]?([^\'" >]+)', s)
    
    ans = re.findall(r'(?<=<h1><a href=")[\'"]?([^\'" >]+)', urllib2.urlopen(url.format(term=search)).read())[0]
    
    print ans

def q8():
    x = 2
    y = 2
    n = 10
    
    filter(lambda t: 0 in [t % z for z in range(x, y + 1)], range(n)[1:])
def q9():
    grades = [(95, 3), (87, 2)]
    sum([x * y for (x, y) in grades]) / sum([y for (x, y) in grades])

def q10():
    text = "a b cde "
    re.sub("\s\s+" , " ", text.lower().title())
      
def q11():
    #can we assume matrix not empty and matrix[0] not empty?
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    matrix = [[], [], []]
    {i : tuple([x[i] for x in matrix]) for i in range(len(matrix[0]))}
def q12():
    x = 7
    reduce(lambda i, j:i * j, range(1, x + 1), 1)

def q13():
    password = 'abcdefg'
    (len(password) > 6) & bool(re.match('^[a-zA-z](?=.*[0-9])(?!.* )', password))    
    
def q14():
    lang = ['abc', 'aa', '1', '10', '11000', '1010']
    filter(lambda st:bool(re.match('^[0-1]*$', st)) & (len(st) % 2 == 0) & (len(re.findall('1', st)) % 2 == 0), lang)

def q15():
    text = "abcdefghijklmnop"    
    "".join(re.findall('(?<=[A-H|J-Z]{3}).(?=[A-H|J-Z]{3})', text))

def q16():
    line = "1| blabla()"    
    re.match('^[0-9]+ (|)', line)
if __name__ == '__main__':
    q7()
    
